package codeRandomThoughts.Test42接雨水;

import java.util.Arrays;
import java.util.Queue;
import java.util.Stack;

//双指针法,比较暴力
//按列计算
public class Solution {
    public static int trap(int[] height) {
        //每列雨水的高度等于左侧最高的柱子高度和右侧最高的柱子高度的最小值

        int sum = 0;//雨水总和
        for (int i = 1; i < height.length - 1; i++) {//第一列和最后一列不算
            //求左边最大值
            int leftMax = 0;
            for (int j = 0; j <= i; j++) {
                if (height[j] > leftMax) {
                    leftMax = height[j];
                }
            }
            //求右边最大值
            int rightMax = 0;
            for (int j = i; j < height.length; j++) {
                if (height[j] > rightMax) {
                    rightMax = height[j];
                }
            }

            int shortHeight = Math.min(leftMax, rightMax);
            sum += (shortHeight - height[i]);
        }

        return sum;
    }

    public static void main(String[] args) {
        int[] height = {4, 2, 0, 3, 2, 5};
        System.out.println(trap(height));
    }
}
